Components.utils.import("resource://mldonkeyff/MLDonkeyDBConfig.js");
Components.utils.import("resource://mldonkeyff/MLDonkeyClient.js");
Components.utils.import("resource://mldonkeyff/MLDonkeyProtocol.js");

var MLDonkeyServersWindowController = {
		onLoad: function() {
			dump("MLDonkeyServersWindow: " + "onLoad: \r\n");
			var a = window.location.href.split("?")[1].split("&");
			var o = {}; 
			a.forEach(
					function(e) {
						o[e.split("=")[0]] = e.split("=")[1];
					}
				);
			if(o["id"] != null) {
				var id = o["id"];
				dump("MLDonkeyServersWindow: " + "onLoad: " + id + "\r\n");
				//
				var client = MLDonkeyDBConfig.getConfiguredClients()[id];
				if(client != null && client.isConnected()) {
					dump("MLDonkeyServersWindow: " + "client.isConnected(): " + client.isConnected() + "\r\n");
					
					client.sendMessage(MLDonkeyProtocol.SEND_GET_CONNECTED_SERVERS);
					
					var servers = client.getMLDonkeyData().serversInfo;
					
					dump("MLDonkeyServersWindow: " + "servers.size(): " + servers.getSize() + "\r\n");

//					for(i in servers) {
//						//dump("MLDonkeyServersWindow: " + "##### servers." + i + " = " + servers[i]);
//						dump("MLDonkeyServersWindow: " + "##### servers." + i + "\r\n");
//					}
					//
					var serversWindowTreeView = {
						treeBox: null,
						selection: null,
						rowCount: servers.getSize(),
						setTree: function(treeBox) {
							dump("MLDonkeyServersWindow: " + "setTree: " + treeBox + "\r\n");
							this.treeBox = treeBox;
						},
						getCellText: function(idx, column) {
							var ret = null;
							try {
								var values = servers.values();
								switch(column.index) {
									case  0: ret = values[idx].identifier; break;
									case  1: ret = values[idx].networkIdentifier; break;
									//se  2: ret = values[idx].internetAddress; break;
									case  3: ret = values[idx].serverPort; break;
									case  4: ret = values[idx].core; break;
									case  5: ret = values[idx].metadata; break;
									case  6: ret = values[idx].numberOfUsers; break;
									case  7: ret = values[idx].numberOfFiles; break;
									//se  8: ret = values[idx].connectionState; break;
									case  9: ret = values[idx].serverName; break;
									case 10: ret = values[idx].serverDescription; break;
									case 11: ret = values[idx].preferred; break;
									case 12: ret = values[idx].serverVersion; break;
									case 13: ret = values[idx].maxUsers; break;
									case 14: ret = values[idx].lowIDUsers; break;
									case 15: ret = values[idx].softLimit; break;
									case 16: ret = values[idx].hardLimit; break;
									case 17: ret = values[idx].ping; break;
									default: ret = ""; break;
								}
							} catch(err) {
								dump("MLDonkeyServersWindow: " + "Error in getCellText(" + idx + ", " + column.index + "): " + err + "\r\n");
								ret = "";
							}
							dump("MLDonkeyServersWindow: " + "getCellText: {" + idx + ", " + column.index + "}... '" + ret + "'\r\n");
							return ret;
						},
						getColumnProperties: function(index, properties) {
							dump("MLDonkeyServersWindow: " + "getColumnProperties: " + index + " " + properties + "\r\n");
						},
						isContainer: function(idx) {
							dump("MLDonkeyServersWindow: " + "isContainer: " + idx + "\r\n");
							return false;
						},
						isContainerOpen: function(idx) {
							dump("MLDonkeyServersWindow: " + "isContainerOpen: " + idx + "\r\n");
							return false;
						},
						isContainerEmpty: function(idx) {
							dump("MLDonkeyServersWindow: " + "isContainerEmpty: " + idx + "\r\n");
							return false;
						},
						isSeparator: function(idx) {
							dump("MLDonkeyServersWindow: " + "isSeparator: " + idx + "\r\n");
							return false;
						},
						isSorted: function() {
							dump("MLDonkeyServersWindow: " + "isSorted\r\n");
							return false;
						},
						isEditable: function(idx, column) {
							dump("MLDonkeyServersWindow: " + "isEditable: " + idx + " " + column + "\r\n");
							return false;
						},
					};
					//
					document.getElementById("serversWindowTree").view = serversWindowTreeView;
				} else {
					alert("Errore: il client non esiste o è disconnesso");
				}
			} else {
				alert("Errore: parametri errati");
			}
		},
}